package com.xx.service.Impl;

import com.xx.dao.auctionuser.UserDao;
import com.xx.entity.AuctionUser;
import com.xx.entity.VO.AuctionUserVo;
import com.xx.service.UserService;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;

/**
 * @author 许欣
 * 时间：2022/4/12 10:55
 * 描述：
 */
public class UserServiceImpl implements UserService{
    @Override
    public AuctionUser getUser(String userName) throws IOException {
        String resource = "mybatis-config.xml";
        InputStream is = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        UserDao userDao=sqlSession.getMapper(UserDao.class);
        AuctionUser user=userDao.getUser(userName);
        sqlSession.commit();
        sqlSession.close();

        return user;
    }

    @Override
    public int add(AuctionUserVo actionUser) throws IOException {
        String resource = "mybatis-config.xml";
        InputStream is = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        UserDao userDao=sqlSession.getMapper(UserDao.class);
        int ret=userDao.add(actionUser);
        sqlSession.commit();
        sqlSession.rollback();
        sqlSession.close();
        return ret;
    }
}
